package com.winbomb.kingcard;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;

import com.winbomb.kingcard.framework.Screen;
import com.winbomb.kingcard.framework.impl.AndroidGame;
import com.winbomb.kingcard.screens.LoadingScreen;

public class KingCard extends AndroidGame {

	public static final String SETTING = "setting";
	public static final String KEY_MUSIC_ON = "isMusicOn";
	public static final String KEY_SOUND_ON = "isSoundOn";

	public static boolean isMusicOn;
	public static boolean isSoundOn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 读取配置
		SharedPreferences setting = getSharedPreferences(SETTING, 0);
		isMusicOn = setting.getBoolean(KEY_MUSIC_ON, true);
		isSoundOn = setting.getBoolean(KEY_SOUND_ON, true);

	}

	@Override
	public Screen getStartScreen() {
		return new LoadingScreen(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		// 按下键盘上返回按钮

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("提示");
			builder.setMessage("确实要退出吗?");
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					finish();
				}
			});

			builder.show();

			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Assets.bg_music.stop();

		// 写回配置文件
		SharedPreferences setting = getSharedPreferences(SETTING, 0);
		Editor editor = setting.edit();
		editor.putBoolean(KEY_MUSIC_ON, isMusicOn);
		editor.putBoolean(KEY_SOUND_ON, isSoundOn);
		editor.commit();

		System.exit(0);
	}

}